package com.crl.rcdp.sys.dao;

import com.cars.util.page.PageUtil;
import com.cars.util.string.StringUtil;
import com.crl.dao.BaseDao;
import com.crl.rcdp.sys.model.SysApp;
import com.crl.rcdp.sys.vo.SysAppSearchVO;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @description:
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/7/12 11:42
 */
@Repository
public class SysAppDao2 extends BaseDao<SysApp, SysAppSearchVO> {


    /**
     * 查询全部
     */
    public List<SysApp> list(SysAppSearchVO sysAppSearchVO) {
        String sql = "SELECT * FROM td_sys_app where 1=1 ";
        sql += createSQL(sysAppSearchVO);
        sql += " order by created_at";
        sql = PageUtil.createOraclePageSQL(sql, sysAppSearchVO.getPage());
        return list(sql, sysAppSearchVO);
    }

    /**
     * 增加API
     */
    public int add(SysApp sysApp) {
        String sql = "INSERT INTO td_sys_app (appid,appsecrect,ip,app_name,contacts,mobile,status,valid_date,remarks,auth_type,created_by,created_at)" +
                " VALUES(:appid,:appsecrect,:ip,:appName,:contacts,:mobile,:status,:validDate,:remarks,:authType,:createdBy,sysdate)";
        return insert(sql, sysApp);
    }

    /**
     * 根据APIid删除
     */
    public int delete(String appid) {
        String sql = "DELETE FROM td_sys_app WHERE appid=?";
        return update(sql, appid);
    }

    /**
     * 根据APIid修改
     */
    public int update(SysApp sysApp) {
        String sql = "UPDATE td_sys_app SET ip=:ip,app_name=:appName,contacts=:contacts,mobile=:mobile,status=:status," +
                "valid_date=:validDate,remarks=:remarks,auth_type=:authType,modified_by=:modifiedBy,modified_at=:modifiedAt WHERE appid=:appid ";
        return update(sql, sysApp);
    }

    /**
     * 统计查询数据条数
     */
    public int count(SysAppSearchVO sysAppSearchVO) {
        String sql = "SELECT COUNT(*) FROM td_sys_app WHERE 1=1 ";
        sql += createSQL(sysAppSearchVO);
        return count(sql, sysAppSearchVO);
    }

    /**
     * 拼接查询条件
     */
    private String createSQL(SysAppSearchVO sysAppSearchVO) {
        String sql = "";
        if (StringUtil.isNotNullOrEmpty(sysAppSearchVO.getAppid())) {
            sql += " and appid=:appid";
        }
        if (StringUtil.isNotNullOrEmpty(sysAppSearchVO.getAppName())) {
            sql += " and app_name like '%'||:appName||'%'";
        }
        return sql;
    }

    /**
     * 根据id查询
     */
    public SysApp get(String appid) {
        String sql = "SELECT * FROM td_sys_app WHERE appid=?";
        return get(sql, appid);
    }

    /**
     * 按照appsecrect进行验证
     *
     * @param appid
     * @param appsecrect
     * @return
     */
    public SysApp getBySecrect(String appid, String appsecrect) {
        String sql = "SELECT * FROM td_sys_app WHERE appid=? and appsecrect=?";
        return get(sql, appid, appsecrect);
    }

    /**
     * 根据id获取下级节点个数
     */
    public int getChildrenCount(Integer parentId) {
        String sql = "SELECT COUNT(id) FROM td_sys_app WHERE parent_id=?";
        return count(sql, parentId);
    }

    /**
     * 修改accessToken
     *
     * @param appid
     * @param accessToken
     * @return
     */
    public int updateAccessToken(String appid, String accessToken) {
        String sql = "update td_sys_app set access_token=?,access_token_at=sysdate where appid=?";
        return update(sql, accessToken, appid);
    }

    /**
     * 修改refreshToken
     *
     * @param appid
     * @param refreshToken
     * @return
     */
    public int updateRefreshToken(String appid, String refreshToken) {
        String sql = "update td_sys_app set refresh_token=?,refresh_token_at=sysdate where appid=?";
        return update(sql, refreshToken, appid);
    }
}
